<template>
  <div class="home">
    <nav-bar class="home-nav"><div slot="center">首页</div></nav-bar>
    <tab-control :titles="titles" @tabClick="tabClick" ref="controlone" class="tab-fixed" v-show="isTabFixed" />

    <scroll class="content" ref="scroll" :probeType="3" @scroll="positionScroll" :pull-up-load="true" @pullingUp="loadMore" >
      <home-swiper :banners= "banners" @swiperImageLoad="swiperImageLoad" />
      <recommend-view :recommends="recommends" />
      <feature-view />
      <tab-control :titles="titles" @tabClick="tabClick" ref="controltwo" />
      <goods-list :goods="showGoods" />
    </scroll>

    <back-top v-show="isShowBackTop" @click.native="backClick" />
  </div>
</template>
<script>
import HomeSwiper from './childComps/HomeSwiper';
import RecommendView from './childComps/RecommendView'
import FeatureView from './childComps/FeatureView';

import NavBar from 'components/common/navbar/NavBar';
import TabControl from 'components/content/tabControl/TabControl';
import GoodsList from 'components/content/goodsList/GoodsList';
import Scroll from 'components/common/scroll/Scroll';
// import BackTop from 'components/content/backTop/BackTop';

import {getHomeMultidata, getHomeGoods} from 'network/home';
import {itemListenerMixin,backTopMixin } from 'common/mixin';

export default {
  name: 'Home',
  components: {
    HomeSwiper,
    RecommendView,
    FeatureView,
    TabControl,
    GoodsList,
    NavBar,
    Scroll,
  },
  mixins: [itemListenerMixin,backTopMixin],
  data() {
    return {
      banners: [],
      recommends: [],
      titles: ['流行', '新款', '精选'],
      goods: {
        'pop':{page:0, list: []},
        'new':{page:0, list: []},
        'sell':{page:0, list: []}
      },
      currentType: 'pop',
      isShowBackTop: false,
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0
    }
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list
    }
  },
  activated(){ // 进入时
    this.$refs.scroll.scrollTo(0, this.saveY, 0)
    this.$refs.scroll.refresh()
  },
  deactivated(){ // 离开时
    // 保存位置
    this.saveY = this.$refs.scroll.getScrollY()
    // 取消全局事件的监听
    this.$bus.$off('itemImageLoad',this.itemImgListener)
  },
  created() {
    // 请求多个数据
    this.getHomeMultidata()
    // 请求商品
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')
  },
  mounted(){
    // const refreshImg = debounce(this.$refs.scroll.refresh,200)
    // // 对监听事件进行保存：
    // this.itemImgListener =  () => {
    //   // this.$refs.scroll.refresh() //每一次加载图片就刷新获取高度
    //   refreshImg()
    // }
    //  // 监听item中图片加载完成
    // this.$bus.$on('itemImageLoad',this.itemImgListener)

    // 获取tabControl的offsetTop
    // console.log(this.$refs.control.$el.offsetTop)
  },
  methods: {
    /*
    *事件监听方法
    */
  
    //点击流行、新款、精选时
    tabClick(index) {
      // console.log(index)
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break
        case 1:
          this.currentType = 'new'
          break
        case 2:
          this.currentType = 'sell'
          break
      }
      if(this.$refs.controlone.currentIndex !== undefined){
        this.$refs.controlone.currentIndex = index 
        this.$refs.controltwo.currentIndex = index
      }
       
    },

    // backClick (){ //点击回到顶部
    //   console.log('backtop')
    //   this.$refs.scroll.scrollTo(0 , 0, 500)
    // },

    positionScroll(position){ //滚动监听到的地址
      // 判断backTop是否显示
      // this.isShowBackTop = (-position.y) > 1000
      this.listenShowBackTop(position)

      // 决定control是的吸顶
      this.isTabFixed = (-position.y) > this.tabOffsetTop
    },

    loadMore(){// 上拉加载更多
      this.getHomeGoods(this.currentType)
    },

    swiperImageLoad() { //swiper图片加载监听
      this.tabOffsetTop = this.$refs.controltwo.$el.offsetTop
    },
    /*
    *网络请求相关方法
    */    
    getHomeMultidata(){ //首页渲染数据
      getHomeMultidata().then(res =>{
        // console.log(res)
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      })
    },
    getHomeGoods(type){ //流行、新款、精选内容渲染出
      const page = this.goods[type].page + 1
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list)
        this.goods[type].page += 1
        //完成上拉加载更多
        this.$refs.scroll.finishPullUp()
      })
    },

  }
}
</script>
<style scoped>
.home{
  position: relative;
  height: 100vh;
}
.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /* position: fixed;
  left: 0;
  top: 0;
  right: 0;
  z-index: 10; */
}
.content{ 
  position: absolute;
  overflow: hidden;
  left: 0;
  top: 44px;
  right: 0;
  bottom: 49px;
}
.tab-fixed{
  position: relative;
  z-index: 10;
  background: #fff;
}
</style>
